package com.tjsinfo.tjsoa.addresslist.action;

import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.addresslist.bo.IAddressListAndFilePendingBo;
import com.tjsinfo.tjsoa.addresslist.bo.IAddresslistSendBo;
import com.tjsinfo.tjsoa.addresslist.vo.AddressListAndFilePending;
import com.tjsinfo.tjsoa.common.DESPlus;
import com.tjsinfo.tjsoa.message.TjsMessageIm;
import com.tjsinfo.tjsoa.system.vo.User;

public class SendAddressListUserAction extends Action{
	private IAddresslistSendBo sendBo;
	private IAddressListAndFilePendingBo addressListAndFilePendingBo;
	public void setAddressListAndFilePendingBo(
			IAddressListAndFilePendingBo addressListAndFilePendingBo) {
		this.addressListAndFilePendingBo = addressListAndFilePendingBo;
	}
	
	public void setSendBo(IAddresslistSendBo sendBo) {
		this.sendBo = sendBo;
	}
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		String moduleType = request.getParameter("moduleType");
		String copyid = request.getParameter("copyid");
		String sendusers = request.getParameter("sendusers");

		if (sendusers != null && !"".equals(sendusers)) {
			HttpSession session = request.getSession();
			User user = (User) session.getAttribute("user");
			String str = sendBo.addListUsersJDBC(sendusers, copyid, user.getUser_id());
			str = str.substring(0,str.length()-1);
			
			String[] strs = sendusers.split(",");
			for (int i = 0; i < strs.length; i++) {
				AddressListAndFilePending message = new AddressListAndFilePending();
				String uuid = UUID.randomUUID().toString().replace("-", "");
				message.setId(uuid);
				message.setSendUserId(user.getUser_id());
				message.setSendUserName(user.getUsername());
				message.setCreateTime(new Date());
				message.setTjsUserId(Integer.valueOf(strs[i]));
				message.setTitle("发送联系人");
				message.setType("通讯录");
				String path = request.getContextPath();
				String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/tjsinfo/tjsoa/addresslist/toReturnSendAddressListUser.do?str=";
				String str1 = ""+str+"#"+copyid+"#"+strs[i]+"";
				try {
					DESPlus plus = new DESPlus();
					str1 = plus.encrypt(str1);
				} catch (Exception e1) {
				}
				basePath += str1;
				basePath += "&messageid="+uuid;
				message.setUrl(basePath);
				addressListAndFilePendingBo.save(message);

				TjsMessageIm messageIm = new TjsMessageIm();
				messageIm.sendMessage(strs[i], user.getUsername()+"向您发送通讯录中联系人");
			}
		}
		request.setAttribute("state", "return");
		request.setAttribute("moduleType", moduleType);
		return mapping.findForward("ok");
	}
}
